<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="目标名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入目标名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['goal:goal:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['goal:goal:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['goal:goal:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleXml">流程图</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['goal:goal:export']"
        >导出</el-button>
      </el-col> -->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="goalList" @selection-change="handleSelectionChange" row-key="id"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <!-- <el-table-column type="expand">
                <template slot-scope="{ row, column, $index }">
                    <el-table v-loading="loading2" :data="row.children" >
                        <el-table-column label="规则名称" align="center" prop="name" />
                        <el-table-column label="所属目标" align="center" prop="goalName" />
                        <el-table-column label="显示顺序" align="center" prop="orderNum" />
                        <el-table-column label="话术" align="center" prop="verbalTrick" />
                        <el-table-column label="下游目标" align="center" prop="nextGoalName" />
                    </el-table>
                </template>
            </el-table-column> -->
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="目标类型" align="center" prop="type">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.goal_type" :value="scope.row.type" />
                </template>
            </el-table-column>
            <el-table-column label="目标名称" align="center" prop="name" />
            <el-table-column label="角色描述" align="center" prop="roleDesc" />
            <el-table-column label="显示顺序" align="center" prop="orderNum" />
            <el-table-column label="创建人" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleRule(scope.row)"
                        v-hasPermi="['goal:goal:edit']">配置规则</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['goal:goal:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['goal:goal:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改中枢目标对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px"  :close-on-click-modal="false" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="目标名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入目标名称" />
                </el-form-item>
                <el-form-item label="目标类型" prop="type">
                    <el-select v-model="form.type" clearable placeholder="请选择目标类型" style="width: 100%">
                        <el-option v-for="item in dict.type.goal_type" :key="item.id" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc" v-if="form.type == 'start'">
                    <el-input v-model="form.roleDesc" type="textarea"  placeholder="请输入目标名称" />
                </el-form-item>
                <el-form-item label="显示顺序" prop="orderNum">
                    <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <el-dialog :visible.sync="bpmnOpen" width="80%" :close-on-click-modal="false" append-to-body>
            <bpmn-viewer :bpmnOpen="bpmnOpen"></bpmn-viewer>
        </el-dialog>

        <el-dialog title="配置规则" :visible.sync="open2" width="80%" :close-on-click-modal="false" append-to-body>
            <rule-index :goalId="goalId"></rule-index>
        </el-dialog>
    </div>
</template>

<script>
import { listGoal, getGoal, delGoal, addGoal, updateGoal,xmlGoal } from "@/api/goal/goal";
import { listRule } from "@/api/goal/rule";
import ruleIndex from './rule/index.vue'
import bpmnViewer from './bpmnViewer.vue'

export default {
    name: "Goal",
    components: {
        ruleIndex,bpmnViewer
    },
    dicts: ['goal_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            loading2: false,
            // 选中数组
            ids: [],
            goalId: null,
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 中枢目标表格数据
            goalList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            bpmnOpen: false,
            open2: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null,
                orderNum: null,
                type: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "目标名称不能为空", trigger: "blur" }
                ],
                type: [
                    { required: true, message: "目标类型不能为空", trigger: "change" }
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询中枢目标列表 */
        getRules(row, expanded) {
            this.loading2 = true
            listRule({ notPage: true , goalId: row.id}).then(res => {
                row.children = res.rows
                this.loading2 = false
            })
        },
        getList() {
            this.loading = true;
            listGoal(this.queryParams).then(response => {
                this.goalList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        handleXml() {
            this.bpmnOpen = true
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                orderNum: null,
                type: null,
                roleDesc: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加中枢目标";
        },
        handleRule(row) {
            this.goalId = row.id
            this.open2 = true;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getGoal(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改中枢目标";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if(this.form.type != 'start') this.form.roleDesc = ''
                    if (this.form.id != null) {
                        updateGoal(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addGoal(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除中枢目标编号为"' + ids + '"的数据项？').then(function () {
                return delGoal(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('goal/goal/export', {
                ...this.queryParams
            }, `goal_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
